package com.doone.telmarket.action.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.doone.telmarket.domain.Activity;
import com.doone.telmarket.service.ActivityOperation;
import com.doone.telmarket.service.SiteResourceManager;
import com.opensymphony.xwork2.ActionContext;


/**
 * @package com.doone.telmarket.action.admin
 * @author 甘雨波
 * @description 精彩促销推荐
 * @date 2009-10-26
 */
public class ActivityAction extends BaseActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Logger log = Logger.getLogger(ServiceAction.class);
	
	@Autowired
	private ActivityOperation remoteActivityOperation;
	
	private String errorMessage;
	
	private String updateType;
	
	private File file;
	
	private String title1;
	private String title2;
	private String title3;
	private String url1;
	private String url2;
	private String url3;
	private String text1;
	private String text2;
	private String text3;
	private String id1;
	private String id2;
	private String id3;
	
	@Autowired
	private SiteResourceManager siteResourceManager;
	
	@Action(value = "selectActivity", results = {
			@Result(name = "success", location = "/admin/templete/setActivity.jsp"),@Result(name = "error", location = "/admin/error.jsp")})
	public String selectActivity(){
		try{
			//log.info("查询精彩促销");
			
			List<Activity> list  = remoteActivityOperation.selectActivity();
			//log.info("查询精彩促销"+list.size());
			session.put("list", list);
			return "success";
		}catch (Exception e) {
			e.printStackTrace();
			errorMessage = "获取数据失败";
			return "error";
		}
		
	}
	@Action("selectActivityAjax")
	public void selectActivityAjax(){
		try{
			//log.info("查询精彩促销");
			List<Activity> list  = remoteActivityOperation.selectActivity();
			//log.info("查询精彩促销"+list.size());
			JSONUtil json = new JSONUtil();
			responseWrite(json.serialize(list));
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
	}
	
	@Action(value = "updateImage", interceptorRefs = { @InterceptorRef("fileUploadStack") })
	public void updateImage() throws IOException{
		String name = "";
		if("1".equals(updateType)){
			name = "images/logo/1.gif";
		}else if("2".equals(updateType)){
			name = "images/logo/2.gif";
		}else{
			name = "images/logo/3.gif";
		}
		
		ActionContext ac = ActionContext.getContext();
		ServletContext sc = (ServletContext) ac
				.get(StrutsStatics.SERVLET_CONTEXT);
		String path = sc.getRealPath("/");
			// 写入文件
		
		FileOutputStream fos = new FileOutputStream(path + "/"+name);
		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		if (fis != null) {
			fis.close();
		}
		if (fos != null) {
			fos.close();
		}
		//siteResourceManager.uploadFile(file, name);
	}
	
	@Action("saveOperation")
	public void saveOperation(){
		try{
			List<Activity> list  = (List)session.get("list");
			list.get(0).setTitle(title1);
			list.get(0).setUrl(url1);
			list.get(0).setContent(text1);
			
			list.get(1).setTitle(title2);
			list.get(1).setUrl(url2);
			list.get(1).setContent(text2);
			
			list.get(2).setTitle(title3);
			list.get(2).setUrl(url3);
			list.get(2).setContent(text3);
			
			for(int i=0;i<list.size();i++){
				remoteActivityOperation.updateActivity(list.get(i));
			}
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getLocalAddr());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());
			pagePath.append("/admin/templete/activity.jsp");
			
			ServletContext sc = (ServletContext) ac
					.get(StrutsStatics.SERVLET_CONTEXT);
			String path = sc.getRealPath("/");
			generatePage(path,pagePath.toString());
			
			responseWrite("保存成功");
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("保存失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
	}
	
	private void generatePage(String rootPath,String pagePath) throws IOException{
		siteResourceManager.writePage(pagePath, rootPath+"/web/common/activity.html");
	}

	public String getErrorMessage() {
		return errorMessage;
	}


	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getUpdateType() {
		return updateType;
	}

	public void setUpdateType(String updateType) {
		this.updateType = updateType;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getTitle1() {
		return title1;
	}
	public void setTitle1(String title1) {
		this.title1 = title1;
	}
	public String getTitle2() {
		return title2;
	}
	public void setTitle2(String title2) {
		this.title2 = title2;
	}
	public String getTitle3() {
		return title3;
	}
	public void setTitle3(String title3) {
		this.title3 = title3;
	}
	public String getUrl1() {
		return url1;
	}
	public void setUrl1(String url1) {
		this.url1 = url1;
	}
	public String getUrl2() {
		return url2;
	}
	public void setUrl2(String url2) {
		this.url2 = url2;
	}
	public String getUrl3() {
		return url3;
	}
	public void setUrl3(String url3) {
		this.url3 = url3;
	}
	public String getText1() {
		return text1;
	}
	public void setText1(String text1) {
		this.text1 = text1;
	}
	public String getText2() {
		return text2;
	}
	public void setText2(String text2) {
		this.text2 = text2;
	}
	public String getText3() {
		return text3;
	}
	public void setText3(String text3) {
		this.text3 = text3;
	}
	public String getId1() {
		return id1;
	}
	public void setId1(String id1) {
		this.id1 = id1;
	}
	public String getId2() {
		return id2;
	}
	public void setId2(String id2) {
		this.id2 = id2;
	}
	public String getId3() {
		return id3;
	}
	public void setId3(String id3) {
		this.id3 = id3;
	}

	
	

}
